package logic

import (
	"context"
	"renting-demo/goods_srv/goodsclient"
	"strconv"

	"renting-demo/api/internal/svc"
	"renting-demo/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddProductLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddProductLogic {
	return &AddProductLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddProductLogic) AddProduct(req *types.AddProductReq) (resp *types.Data, err error) {
	// todo: add your logic here and delete this line
	res, _ := l.svcCtx.GoodsServer.AddProduct(context.Background(), &goodsclient.AddProductReq{
		GoodsImg:   req.Goodsimg,
		GoodsNum:   req.Goodsnum,
		GoodsPrice: float32(req.Goodsprice),
		GoodsName:  req.Goodsname,
		GoodsText:  req.Goodstext,
	})
	if res.GoodsID == 0 {
		return &types.Data{
			Code:    500,
			Message: "添加失败",
		}, nil
	}

	return &types.Data{
		Code:    0,
		Message: "添加成功",
		Data:    map[string]string{"goodsId": strconv.FormatInt(res.GoodsID, 10)},
	}, nil
}
